<template>
  <div>
    <div class="login-wrap">
      <!-- 登陆标题  -->
      <div class="login-title">
        <h2>商城后台管理系统</h2>
      </div>
      <!-- 登陆框 -->
      <div class="login">
        <h3>管理员登陆</h3>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
          <!-- 账号输入 -->
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" v-model="ruleForm.username"></el-input>
          </el-form-item>
          <!-- 密码输入 -->
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" v-model="ruleForm.password"></el-input>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
// import userApi from "./network/LoginApi";
export default {
  name: "login",
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      // 表单验证
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 11, message: "长度在 3 到 11 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 11, message: "长度在 3 到 11 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        // 判断表单验证是否全部输入
        if (valid) {
          // 发送登陆请求
          this.$http.post('api/login', this.ruleForm).then(res => {
            // 请求数据进行赋值解构
            let { result, info, status } = res.data;
            // 判断是否登陆成功
            if (status === 1) {
              this.$message({
                message: info,
                type: "success"
              });
              // 将登陆用户信息存入session内存中
              sessionStorage.setItem('user',JSON.stringify(result[0]))
              this.$router.push('/')
              return;
            }
            this.$message({
              message: info,
              type: "warning"
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 清空表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style lang=''>
.login-wrap {
  width: 400px;
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}
.login-wrap label {
  text-align: center;
}

/* 登陆标题 */
.login-title {
  display: flex;
  background: #e77817;
  box-sizing: border-box;
  height: 60px;
  width: 300px;
  padding: 10px 20px;
  margin: 0px auto 50px;
  border-radius: 8px;
}
.login-title p {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-title h2 {
  flex: 3;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
  padding: 0;
  font-weight: 500;
}
/* 登陆框 */
.login-wrap h3 {
  border-bottom: 1px solid #dedede;
  padding: 10px 0;
  margin: 0px 0 25px;
}
.el-input i {
  font-size: 20px;
}
</style>